package com.atguigu.edu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.edu.client.VodClient;
import com.atguigu.edu.entity.Video;
import com.atguigu.edu.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-10
 */
@RestController
@RequestMapping("/edu/video")
@CrossOrigin
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody Video video) {
        videoService.save(video);
        return R.ok();
    }

    //删除小节
    @DeleteMapping("/{id}")
    public R deleteVideo(@PathVariable String id) {
        Video video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            //根据视频id删除阿里云视频
            vodClient.removeAliYunVideo(videoSourceId);
        }
        videoService.removeById(id);
        return R.ok();
    }

    //查询小节
    @GetMapping("/getVideoInfo/{videoId}")
    public R getVideoInfo(@PathVariable String videoId) {
        Video video = videoService.getById(videoId);
        return R.ok().data("video",video);
    }
    //更新小节
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody Video video) {
        boolean b = videoService.updateById(video);
        if (b) {
            return R.ok();
        }else {
            return R.error();
        }
    }
}

